package vip.xiaonuo.issued.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.pojo.httpclient.ReturnJson;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParam;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParamRet;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.service.VisitorDeviceService;
import vip.xiaonuo.facedoor.iservice.IVisitorDealWithService;
import vip.xiaonuo.facedoor.remote.iservice.adapter.VisitorReqAdapter;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonResponse;

@Service(value = RuleConstant.DW+ RuleConstant.VISITOR_DEAL_WITH)
public class HttpVisitorDealWithServiceImpl implements IVisitorDealWithService {

    @Autowired
    private VisitorReqAdapter visitorReqAdapter;

    @Autowired
    private VisitorDeviceService visitorDeviceService;

    @Autowired
    private DeviceLogService logService;

    @Autowired
    private VisitorService visitorService;


    @Override
    public PersonBaseParamRet getBaseParam(PersonBaseParam personBasePerson) {
        return new PersonBaseParamRet().setUrl("http://" + personBasePerson.getDeviceBase().getCabinetIp() + ":" + personBasePerson.getDeviceBase().getCabinetPort());
    }

    @Override
    public void deleteException(VisitorDevice visitorDevice, Visitor visitor, DeviceBase deviceBase, Exception e) {
        visitorDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        visitorDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        visitorDeviceService.updateById(visitorDevice);
        logService.addVisitorLog(visitor.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, UPDATE);
    }

    @Override
    public void delete(VisitorDevice visitorDevice, Visitor visitor, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
        ReturnJson returnJson = visitorReqAdapter.deletePerson(visitorDevice, baseParam.getUrl());
        // 删除用户成功
        if (StringUtils.isNotBlank(returnJson.getCode()) && ("0000".equals(returnJson.getCode()) || "0201".equals(returnJson.getCode()))) {
            visitorDeviceService.removeById(visitorDevice.getId());
            long count = visitorDeviceService.count(new LambdaQueryWrapper<VisitorDevice>().eq(VisitorDevice::getVisitorId, visitor.getId()));
            if (count == 0 && Objects.equals(visitor.getDelFlag(), "1")) {
                visitorService.removeById(visitorDevice.getVisitorId());
            }
            logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, DELETE);
        } else {
            visitorDevice.setFailReason(JSON.toJSONString(returnJson));
            visitorDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
            visitorDeviceService.updateById(visitorDevice);
            logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), FAIL, DELETE);
        }
    }

    @Override
    public void add(VisitorDevice visitorDevice, Visitor visitor, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
        // 先查询人员
        GetPersonResponse getPersonResponse = visitorReqAdapter.getPerson(baseParam.getUrl(), visitor);
        // 不存在 新增
        if (StringUtils.isNotBlank(getPersonResponse.getCode()) && "0202".equals(getPersonResponse.getCode())) {
            ReturnJson returnJson = visitorReqAdapter.addPerson(baseParam.getUrl(), visitor);
            if (returnJson.getCode().equals("0000")) {
                logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, ADD);
                visitorDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
                visitorDeviceService.updateById(visitorDevice);
            } else {
                visitorDevice.setFailReason("新增人员发生错误：" + JSON.toJSONString(returnJson));
                visitorDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
                visitorDeviceService.updateById(visitorDevice);
                logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), FAIL, ADD);

            }
            // 修改
        } else if (StringUtils.isNotBlank(getPersonResponse.getCode()) && "0000".equals(getPersonResponse.getCode())) {
            ReturnJson returnJson = visitorReqAdapter.updatePerson(baseParam.getUrl(), visitor);
            if (returnJson.getCode().equals("0000")) {
                visitorDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
                visitorDeviceService.updateById(visitorDevice);
                logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, UPDATE);

            } else {
                visitorDevice.setFailReason("修改人员发生错误：" + JSON.toJSONString(returnJson));
                visitorDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
                visitorDeviceService.updateById(visitorDevice);
                logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), FAIL, UPDATE);

            }
        } else {
            visitorDevice.setFailReason("查询人员发生错误：" + JSON.toJSONString(getPersonResponse));
            visitorDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
            visitorDeviceService.updateById(visitorDevice);
            logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(getPersonResponse), FAIL, ADD);

        }
    }

    @Override
    public void addException(VisitorDevice visitorDevice, Visitor visitor, DeviceBase deviceBase, Exception e) {
        visitorDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        visitorDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        visitorDeviceService.updateById(visitorDevice);
        logService.addVisitorLog(visitor.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, ADD);
    }

    @Override
    public void updateException(VisitorDevice visitorDevice, Visitor visitor, DeviceBase deviceBase, Exception e) {
        visitorDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        visitorDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        visitorDeviceService.updateById(visitorDevice);
        logService.addVisitorLog(visitor.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, UPDATE);
    }

    @Override
    public void update(VisitorDevice visitorDevice, Visitor visitor, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
        ReturnJson returnJson = visitorReqAdapter.updatePerson(baseParam.getUrl(), visitor);
        if (StringUtils.isNotBlank(returnJson.getCode()) && ("0000".equals(returnJson.getCode()) || "0201".equals(returnJson.getCode()))) {
            logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, UPDATE);
            visitorDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
            visitorDeviceService.updateById(visitorDevice);
        } else if (returnJson.getCode().equals("0202")) {
            ReturnJson addRj = visitorReqAdapter.addPerson(baseParam.getUrl(), visitor);
            if (addRj.getCode().equals("0000")) {
                visitorDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
                visitorDeviceService.updateById(visitorDevice);
                logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, ADD);
            } else {
                logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), FAIL, ADD);
                visitorDevice.setFailReason(JSON.toJSONString(returnJson));
                visitorDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
                visitorDeviceService.updateById(visitorDevice);
            }
        } else {
            visitorDevice.setFailReason(JSON.toJSONString(returnJson));
            visitorDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
            visitorDeviceService.updateById(visitorDevice);
            logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), FAIL, UPDATE);
        }
    }

}
